﻿//-----------------------------------------------------------------------
// <copyright file="FilesystemIO.cs" author="Lennart">
//     License: MIT
// </copyright>
//-----------------------------------------------------------------------
namespace UtilityMethods {
    using System.Diagnostics;
    using System.IO;

    /// <summary>
    /// Includes functions to manage files and folders.
    /// </summary>
    public static class FilesystemIO {
        /// <summary>
        /// Creates a Directory if it does not exist.
        /// </summary>
        /// <param name="path">The directory path to check.</param>
        /// <param name="create">If the directory should be created.</param>
        /// <returns>True if the directory exists, False if not.</returns>
        public static bool DoesExist(string path, bool create) {
            if (!Directory.Exists(path)) {
                if (create) {
                    Directory.CreateDirectory(path);
                    return true;
                }

                return true;
            }

            return false;
        }

        /// <summary>
        /// Gets the file description of a given file.
        /// </summary>
        /// <param name="targetFile">The filename to check.</param>
        /// <param name="targetDirectory">The directory the file is in.</param>
        /// <param name="returnFilnameIfEmpty">If the file has no description, whether to return the file name instead.</param>
        /// <returns>Either the description of the file, the filename or an empty string.</returns>
        public static string GetFileDescription(string targetFile, string targetDirectory, bool returnFilnameIfEmpty) {
            FileVersionInfo fileInfo = FileVersionInfo.GetVersionInfo(targetDirectory + "\\" + targetFile);
            if (fileInfo.FileDescription != null) {
                return fileInfo.FileDescription;
            }
            else {
                if (returnFilnameIfEmpty) {
                    return targetFile;
                }
                else {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// Resolves environment variables to their actual path.
        /// </summary>
        /// <param name="variable">A string containing an environment variable.</param>
        /// <returns>The resolved string.</returns>
        public static string ResolveVariables(string variable) {
            return System.Environment.ExpandEnvironmentVariables(variable);
        }
    }
}
